var express = require('express');
var router = express.Router();
var User = require('../db/user.js');
var article = require('../db/article.js');
var dynamic = require('../db/dynamic.js');
var Collection = require('../db/collections');



//关注专题
router.post("/att_on_coll", function (req, res, next) {
  Collection.findOne({ collectionid: req.body.cid }, function (err, data) {
    if (err) { console.log(err); return; }
    if (data.fansList.length > 0) {
      for (let i = 0; i < data.fansList.length; i++) {
        if (req.cookies.userID == data.fansList[i]) {
          res.send("你已经收藏过了哦"); return;
        }
      }
    }
    Collection.updateOne({ collectionid: req.body.cid }, { $addToSet: { fansList: req.cookies.userID } }, function (err) {
      if (err) { console.log(err); return; }
      User.updateOne({ uid: req.cookies.userID }, { $addToSet: { attentionFile: req.body.cid } }, function (err) {
        if (err) { console.log(err); return; }
        res.send("关注成功");
      })
    })
  })
});

//专题显示页
router.post("/showCollection", function (req, res, next) {
  Collection.findOne({ collectionid: req.body.cid }, function (err, data) {
    if (err) { console.log(err); }
    if (data.listAid.length > 0) {
      async function getArticles() {
        let Articles = [];
        for (let i = 0; i < data.listAid.length; i++) {
          await article.findOne({ a_id: data.listAid[i] }, function (err, datas) {
            if (err) { console.log(err); }
            Articles.push({
              aid: datas.a_id,
              name: datas.uname,
              title: datas.title,
              intr: datas.intr,
              coverImg: datas.coverImg,
              viewNum: datas.viewCount,
              likeNum: datas.likeCount.length,
              collectionNum: datas.collectionCount.length,
              commentNum: datas.comment.length,
            })
          });
        }
        return Articles;
      }
      getArticles().then(result => {
        // console.log(result);
        res.json({
          filename: data.fileName,
          fansNum: data.fansList.length,
          articlesNum: data.listAid.length,
          AsList: result,
        });
      });
    } else {
      res.json({
        filename: data.fileName,
        fansNum: data.fansList.length,
        articlesNum: data.listAid.length,
        AsList: [],
      });
    }
  })
});


//动态正文显示页
router.post("/showContent2", function (req, res, next) {
  // dynamic.updateOne({ d_id: req.body.did }, { $inc: { viewCount: 1 } }, function (err) {
  //   if (err) { console.log(err); return; }
  // });
  dynamic.findOne({ d_id: req.body.did }, function (err, data) {
    if (err) { console.log(err); return; }
    else {
      // console.log(data);
      User.findOne({ uid: data.uid }, function (err, item) {
        if (err) { console.log(err); return; }
        // console.log(item);
        let text = data.content.replace(new RegExp("\n", 'g'), "<br>");
        let commentList = [];
        if (data.comment.length > 0) {
          for (let i = 0; i < data.comment.length; i++) {
            commentList.push({
              cid: data.comment[i].c_id,
              cname: data.comment[i].c_name,
              cimgHead: "http://localhost:3000/" + data.comment[i].c_imgHead,
              caddtime: data.comment[i].c_addTime,
              ccontent: data.comment[i].c_content,
              clikes: data.comment[i].c_likes.length,
            })
          }
        }
        res.json({
          uid: item.uid,
          name: data.uname,
          headImg: item.img_head,
          addtime: data.showTime,
          content: text,
          picList: data.pictureUrl,
          commentList: commentList,
        })
      })
    }
  });
});

//文章正文显示页
router.post("/showContent", function (req, res, next) {
  article.updateOne({ a_id: req.body.aid }, { $inc: { viewCount: 1 } }, function (err) {
    if (err) { console.log(err); return; }
  });
  article.findOne({ a_id: req.body.aid }, function (err, data) {
    if (err) { console.log(err); return; }
    else {
      // console.log(data);
      User.findOne({ uid: data.uid }, function (err, item) {
        if (err) { console.log(err); return; }
        // console.log(item);
        let commentList = [];
        if (data.comment.length > 0) {
          for (let i = 0; i < data.comment.length; i++) {
            commentList.push({
              cid: data.comment[i].c_id,
              cname: data.comment[i].c_name,
              cimgHead: "http://localhost:3000/" + data.comment[i].c_imgHead,
              caddtime: data.comment[i].c_addTime,
              ccontent: data.comment[i].c_content,
              clikes: data.comment[i].c_likes.length,
            })
          }
        }
        res.json({
          title: data.title,
          uid: item.uid,
          name: data.uname,
          headImg: item.img_head,
          addtime: data.addTime,
          viewCount: data.viewCount,
          content: data.content,
          likeNum: data.likeCount.length,
          collectionNum: data.collectionCount.length,
          filename: data.fileName,
          commentList: commentList,
        })
      })
    }
  });
});

module.exports = router;
